package com.zzzy.mbg;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class MybatisPlusGenerator {
    //项目绝对路径
    public static String projectPath = System.getProperty("user.dir");
    public static Properties properties = initProperties();

    public static void main(String[] args) {
        AutoGenerator generator = new AutoGenerator(getDataSourceConfig());
        //全局配置(GlobalConfig)
        generator.global(getGlobalConfig());
        //包位置配置(PackageConfig) 是否生成相应文件 在 PackageConfig中配置
        generator.packageInfo(getPackageConfig());
        //策略配置(StrategyConfig)
        generator.strategy(getStrategyConfig());
        generator.execute();
    }

    /**
     * 未特殊命名的模板 均为 plus 默认模板
     */
    private static StrategyConfig getStrategyConfig(){
        StrategyConfig.Builder builder = new StrategyConfig.Builder()
                //忽略表前缀
                .addTablePrefix(getProperty("table-prefix"))
                //生成指定表
                .addInclude(getProperty("include-table"));

        //controller 策略 使用自定义模板,是否生成 Controller 在 PackageConfig 中配置
        builder.controllerBuilder().enableRestStyle().template("/vm/java/custom-controller.java");
        //entity 策略
        builder.entityBuilder().disableSerialVersionUID().enableLombok().enableTableFieldAnnotation().idType(IdType.AUTO)
                .javaTemplate("/vm/java/entity.java");
        //service 策略 不生成service接口,重命名ServiceImpl
        builder.serviceBuilder().disableService().formatServiceImplFileName("%sService").serviceImplTemplate("/vm/java/serviceImpl.java");
        //mapper 策略 BaseMapperE 为自定义的mapper
        builder.mapperBuilder().superClass("BaseMapperE").enableBaseColumnList()
                //配置模板 mapper
                .mapperTemplate("/vm/java/mapper.java")
                .mapperXmlTemplate("/vm/xml/mapper.xml");

        return builder.build();
    }
    private static PackageConfig getPackageConfig(){
        Map<OutputFile, String> pathInfoMap = new HashMap<>();
        //位置自定义
        pathInfoMap.put(OutputFile.xml, projectPath + "/" + getProperty("module-name") + "/src/main/resources/mapper/");
        //此处为空则不生成 controller
        if (StringUtils.isBlank(getProperty("controller-package"))){
            pathInfoMap.put(OutputFile.controller, "");
        }
        //为空则不生成 serviceImpl
        if (StringUtils.isBlank(getProperty("serviceImpl-package"))){
            pathInfoMap.put(OutputFile.serviceImpl, "");
        }
        return new PackageConfig.Builder()
                .parent(getProperty("parent-package"))
                .entity("dao.entity")
                .serviceImpl(getProperty("serviceImpl-package"))
                .controller(getProperty("controller-package"))
                .mapper("dao.mapper")
                .pathInfo(pathInfoMap)
                .build();
    }
    private static GlobalConfig getGlobalConfig(){
        return new GlobalConfig.Builder()
                .disableOpenDir()
                .outputDir(projectPath + "/" + getProperty("module-name") + "/src/main/java")
                .author(getProperty("author"))
                //启用 Springdoc
                .enableSpringdoc()
                .dateType(DateType.ONLY_DATE)
                .commentDate("yyyy-MM-dd")
                .build();
    }

    private static DataSourceConfig getDataSourceConfig(){
        return new DataSourceConfig.Builder(getProperty("url"), getProperty("username"), getProperty("password"))
                .keyWordsHandler(new MySqlKeyWordsHandler())
                .build();
    }

    private static Properties initProperties(){
        ClassLoader classLoader = MybatisPlusGenerator.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("mybatis-generator.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            inputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return properties;
    }

    private static String getProperty(String key){
        return properties.getProperty(key);
    }
}
